package com.hongxun.modules.bss.controller;

import java.util.*;

import com.hongxun.modules.api.entity.BDataDictionaryEntity;
import com.hongxun.modules.api.service.BDataDictionaryService;
import com.hongxun.modules.bss.entity.*;
import com.hongxun.modules.bss.service.APBaseService;
import com.hongxun.modules.bss.service.APRangeService;
import com.hongxun.modules.bss.service.DWorkContentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongxun.modules.bss.service.APOtherService;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;




/**
 * 项目扩展信息表
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-08-03 13:46:04
 */
@RestController
@RequestMapping("/bss/apother")
public class APOtherController {
	@Autowired
	private APOtherService aPOtherService;
	@Autowired
	private BDataDictionaryService bDataDictionaryService;
	@Autowired
	private DWorkContentService dWorkContentService;
	@Autowired
	private APRangeService aPRangeService;
	@Autowired
	private APBaseService aPBaseService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        String wheres =" 1=1 ";
		if(query.get("pGuid")!=null&&!query.get("pGuid").toString().equals(""))
		{
			wheres+=" and  p_guid = '" + query.get("pGuid").toString()+"' ";
		}
		if(query.get("state")!=null&&query.get("state").toString().equals("isdelno")) {
			wheres += " and  p_o_datastate != '删除' ";
		}
        query.put("filterSql",wheres+" ");
		List<APOtherEntity> aPOtherList = aPOtherService.queryList(query);
		int total = aPOtherService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(aPOtherList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{guid}")
    public R info(@PathVariable("guid") String guid){


		APOtherEntity aPOther = aPOtherService.queryObject(guid);

		return R.ok().put("aPOther", aPOther);
	}
	@RequestMapping("/infoBypguid/{guid}")
	public R infoBypguid(@PathVariable("guid") String guid){
		Map<String, Object> params= new HashMap<>();
		params.put("pGuid",guid);
		Query query = new Query(params);
		String wheres =" 1=1 ";
		if(query.get("pGuid")!=null&&!query.get("pGuid").toString().equals(""))
		{
			wheres+=" and  p_guid = '" + query.get("pGuid").toString()+"' ";
		}
		wheres += " and  p_o_datastate != '删除' ";

		query.put("filterSql",wheres+" ");
		List<APOtherEntity> aPOtherList = aPOtherService.queryList(query);

		if(aPOtherList.size()>0) {
			return R.ok().put("aPOther", aPOtherList.get(0));
		}
		else
		{
			APOtherEntity aPOtherEntity= new APOtherEntity();
			aPOtherEntity.setPODatastate("新增");
			aPOtherEntity.setPGuid(guid);
			aPOtherService.save(aPOtherEntity);
			List<APOtherEntity> aPOtherList1 = aPOtherService.queryList(query);
			return R.ok().put("aPOther", aPOtherList1.get(0));
		}

	}
	@RequestMapping("/getpOContent")
	public R getpOContent(@RequestBody APOtherEntity aPOther) {
		 String counts = "";
		String wheres =" 1=1 ";
		wheres+=" and  p_guid = '" + aPOther.getPGuid()+"' ";
		Map<String ,Object> mapaPOther = new HashMap<>();
		mapaPOther.put("filterSql",wheres+" ");
		List<APRangeEntity> aPRangeList =aPRangeService.queryList(mapaPOther);

		APBaseEntity  aPBase = aPBaseService.queryObject(aPOther.getPGuid());
		counts = getContentxlandsb(counts, aPRangeList);

		aPOther.setPOContent(counts);
		return R.ok().put("aPOther", aPOther);
	}

	private String getContentxlandsb(String counts, List<APRangeEntity> aPRangeList) {

    	for (APRangeEntity aPRangeEntity : aPRangeList) {
			List<MyContent> myContentxlList = dWorkContentService.getContentxl(aPRangeEntity.getGuid());
			List<MyContent> myContentsbList = dWorkContentService.getContentsb(aPRangeEntity.getGuid());
			Map<String, Object> mapaPOther = new HashMap<>();
			String wheres = " 1=1 ";
			wheres += " and  p_guid = '" + aPRangeEntity.getPGuid() + "' ";
			wheres += " and  w_c_content like '" + aPRangeEntity.getPRName() + "%' ";
			mapaPOther.put("filterSql", wheres + " ");
			List<DWorkContentEntity> dWorkContentList = dWorkContentService.queryList(mapaPOther);


			if (myContentxlList.size() + myContentsbList.size() + dWorkContentList.size() > 0) {
				counts += "【" + aPRangeEntity.getPRName() + "情况】\n";
			}
			else
			{
				counts += "【" + aPRangeEntity.getPRName() + "情况】\n";
				counts += "   无 \n";
			}




			double gtzl=0;
			String  gtzlstr="";
			double gtxl=0;
			String  gtxlstr="";
			double gtlj=0;
			String  gtljstr="";
			double gtcc=0;
			String  gtccstr="";
			double dxjs=0;
			String  dxjsstr="";
			double dxlj=0;
			String  dxljstr="";
			double dxcc=0;
			String  dxccstr="";
			double lxaz=0;
			String  lxazstr="";

			List<String> listsbname = new ArrayList<String>();

			for (MyContent myContent : myContentsbList) {
				boolean hss= false;
				for(String sbname :listsbname) {
                   if(sbname.equals(myContent.getpType()))
					{
						hss=true;
					}
                }
                if(!hss) {
					listsbname.add(myContent.getpType());
				}
			}


				for (MyContent myContent : myContentxlList) {
				if (myContent.getpName().equals(aPRangeEntity.getPRName())
						&& myContent.getpType().equals("杆塔")
						&& myContent.getpPWorktype().equals("组立")) {
					if(myContent.getMx().contains("组合包"))
					{
						gtzlstr += myContent.getMx() + ";";
						gtzl+=(myContent.getZl()*2);
					}
					else {
						gtzlstr += myContent.getMx() + ";";
						gtzl += myContent.getZl();
					}
				}
				if (myContent.getpName().equals(aPRangeEntity.getPRName())
						&& myContent.getpType().equals("杆塔")
						&& myContent.getpPWorktype().equals("修理")) {
					gtxlstr+=myContent.getMx()+";";
					gtxl+=myContent.getZl();
				}
				if (myContent.getpName().equals(aPRangeEntity.getPRName())
						&& myContent.getpType().equals("杆塔")
						&& myContent.getpPWorktype().equals("利旧")) {
					dxljstr+=myContent.getMx()+";";
					dxlj+=myContent.getZl();
				}
				if (myContent.getpName().equals(aPRangeEntity.getPRName())
						&& myContent.getpType().equals("杆塔")
						&& myContent.getpPWorktype().equals("拆除")) {
					gtccstr+=myContent.getMx()+";";
					gtcc+=myContent.getZl();
				}
				if (myContent.getpName().equals(aPRangeEntity.getPRName())
						&& myContent.getpType().equals("导线")
						&& myContent.getpPWorktype().equals("架设")) {
					dxjsstr+=myContent.getMx()+";";
					dxjs+=myContent.getZl();
				}
				if (myContent.getpName().equals(aPRangeEntity.getPRName())
						&& myContent.getpType().equals("导线")
						&& myContent.getpPWorktype().equals("利旧")) {
					dxljstr+=myContent.getMx()+";";
					dxlj+=myContent.getZl();
				}
				if (myContent.getpName().equals(aPRangeEntity.getPRName())
						&& myContent.getpType().equals("导线")
						&& myContent.getpPWorktype().equals("拆除")) {
					dxccstr+=myContent.getMx()+";";
					dxcc+=myContent.getZl();
				}

			}
			int i=1;
			int j=1;
         if(myContentxlList.size()>0) {

			 counts += i+" 配网线路\n";
			 i++;
			 if(gtzl+gtcc+gtlj+gtxl>0)
			 {
				  counts += "("+j+")杆塔情况：\n";
				  j++;
				  if(gtzl>0)
				  {
					  counts += "杆塔组立  合计："+(int)gtzl+"根，其中 " + gtzlstr+"\n";
				  }
				 if(gtxl>0)
				 {
					 counts += "杆塔修理  合计："+(int)gtxl+"根，其中 " + gtxlstr+"\n";
				 }
				 if(gtlj>0)
				 {
					 counts += "杆塔利旧 合计："+(int)gtlj+"根，其中 " +gtljstr+"\n";
				 }
				 if(gtcc>0)
				 {
					 counts += "杆塔拆除  合计："+(int)gtcc+"根，其中 " +gtccstr+"\n";
				 }

			 }
			 if(dxjs+dxcc+dxlj>0)
			 {
				 counts += "("+j+")导线情况：\n";
				 j++;
				 if(dxjs>0)
				 {
					 counts += "导线架设  合计："+dxjs+"米，其中 " +dxjsstr+"\n";
				 }
				 if(dxlj>0)
				 {
					 counts += "导线利旧  合计："+dxlj+"米，其中 " + dxljstr+"\n";
				 }
				 if(dxcc>0)
				 {
					 counts += "导线拆除  合计："+dxcc+"米，其中 " +dxccstr+"\n";
				 }
			 }
			 if(lxaz>0)
			 {
				 counts += "("+j+")拉线情况：\n";
				 j++;
				 counts += "拉线安装  合计："+(int)lxaz+"根，其中 "+ lxazstr+"\n";
			 }


		 }
			if(myContentsbList.size()>0) {

				counts += i+" 配网设备\n";
				i++;
				counts += "（1）设备汇总\n";
				int n=1;
				for(String sbname :listsbname) {
					counts +=  n+")"+sbname +"情况： \n";
					n++;
					double sbxz=0;
					String  sbxzstr="";
					double sblj=0;
					String  sbljstr="";
					double sbcc=0;
					String  sbccstr="";
					double sbxl=0;
					String  sbxlstr="";

					for (MyContent myContent : myContentsbList) {
						if (myContent.getpName().equals(aPRangeEntity.getPRName())
								&& myContent.getpType().equals(sbname)
								&& myContent.getpPWorktype().equals("新装")) {
							sbxzstr += myContent.getMx() + ";";
							sbxz += myContent.getZl();
						}
						if (myContent.getpName().equals(aPRangeEntity.getPRName())
								&& myContent.getpType().equals(sbname)
								&& myContent.getpPWorktype().equals("利旧")) {
							sbljstr += myContent.getMx() + ";";
							sblj += myContent.getZl();
						}
						if (myContent.getpName().equals(aPRangeEntity.getPRName())
								&& myContent.getpType().equals(sbname)
								&& myContent.getpPWorktype().equals("拆除")) {
							sbccstr += myContent.getMx() + ";";
							sbcc += myContent.getZl();
						}
						if (myContent.getpName().equals(aPRangeEntity.getPRName())
								&& myContent.getpType().equals(sbname)
								&& myContent.getpPWorktype().equals("修理")) {
							sbxlstr += myContent.getMx() + ";";
							sbxl += myContent.getZl();
						}
					}
					if(sbxz>0)
					{
						counts += sbname+"新装   合计："+(int)sbxz+"台，其中 "+ sbxzstr+"\n";
					}
					if(sblj>0)
					{
						counts += sbname+"利旧  合计："+(int)sblj+"台，其中 "+sbljstr+"\n";
					}
					if(sbxl>0)
					{
						counts += sbname+"修理  合计："+(int)sbxl+"台，其中 "+sbxlstr+"\n";
					}
					if(sbcc>0)
					{
						counts += sbname+"拆除  合计："+(int)sbcc+"台，其中 "+sbccstr+"\n";
					}

				}




				counts += "（2）设备明细\n";
				int m=1;
				for(DWorkContentEntity dWorkContentEntity:dWorkContentList)
				{
					counts+=   m+")"+dWorkContentEntity.getWCContent()+";\n";
					m++;
				}

			}


		}
		return counts;
	}

	@RequestMapping("/getpODirection")
	public R getpODirection(@RequestBody APOtherEntity aPOther){

		Map<String ,Object> mapaPOther = new HashMap<>();
		String wheres ="1=1";
		wheres+=" and  d_code = 'direction_type' ";
        mapaPOther.put("filterSql",wheres);
		List<BDataDictionaryEntity> bDataDictionaryList = bDataDictionaryService.queryList(mapaPOther);

		String Direction="";
        for(BDataDictionaryEntity bDataDictionaryEntity:bDataDictionaryList)
	   {
		   Direction+=bDataDictionaryEntity.getDValue()+"\n";
	   }
		aPOther.setPODirection( Direction);
		return R.ok().put("aPOther", aPOther);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody APOtherEntity aPOther){
		aPOtherService.save(aPOther);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody APOtherEntity aPOther){
		aPOther.setPODatastate("修改");
		aPOtherService.update(aPOther);

		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] guids){
		aPOtherService.deleteBatch(guids);
		
		return R.ok();
	}

}
